using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using mp3info;
using Orthogonal.TagCommon;
using Orthogonal.NTag;
using System.IO;

namespace MP3Flop
{
    public partial class frmMain : Form
    {
        private delegate void TreeNodeArrayCallback(TreeNode[] nodes);

        private ProcessorManager procman;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            procman = new ProcessorManager();

            try {
                //>> Attempt to start the tagging library
                procman.Startup();
            }
            catch (NTagException ex) {
                throw ex;
            }



            LoadData(new DirectoryInfo(@"C:\Documents and Settings\Me\Desktop\test mp3s\"));
        }

        private struct LoadThreadData
        {
            public DirectoryInfo dir;
            public frmProgress progressForm;
        }

        private void LoadData(DirectoryInfo dir)
        {
            frmProgress progressForm = new frmProgress();
            Thread thread = new Thread(LoadDataThread);
            LoadThreadData data = new LoadThreadData();

            progressForm.Init(CountFiles(dir));

            data.dir = dir;
            data.progressForm = progressForm;

            thread.Priority = ThreadPriority.BelowNormal;
            thread.Start(data);

            progressForm.ShowDialog();
        }

        private void LoadDataThread(object param)
        {
            LoadThreadData data = (LoadThreadData)param;

            AddNodesList1(BuildTree(data.dir, data.progressForm).ToArray());

            data.progressForm.KillForm();
        }

        public void AddNodesList1(TreeNode[] nodes)
        {
            if (this.treeView1.InvokeRequired) {
                TreeNodeArrayCallback d = new TreeNodeArrayCallback(AddNodesList1);
                this.Invoke(d, new object[] { nodes });
            } else {
                this.treeView1.Nodes.AddRange(nodes);
            }
        }

        private List<TreeNode> BuildTree(DirectoryInfo d, frmProgress progressForm)
        {
            List<TreeNode> nodes =  new List<TreeNode>();
            TreeNode node;
            FileData data;

            DirectoryInfo[] dirs = d.GetDirectories();
            for (int i = 0; i < dirs.Length; i++) {
                node = new TreeNode(dirs[i].Name + "\\");
                node.Nodes.AddRange(BuildTree(dirs[i], progressForm).ToArray());
                nodes.Add(node);
            }

            foreach (FileInfo f in d.GetFiles()) {
                data = new FileData();
                data.FileName = f.Name;
                data.FileFullName = f.FullName;

                node = new TreeNode(f.Name);
                node.Tag = data;
                nodes.Add(node);

                progressForm.UpdateCnt();
            }
            return nodes;
        }

       private int CountFiles(DirectoryInfo d)
        {
            int cnt = 0;
            DirectoryInfo[] dirs = d.GetDirectories();
            for (int i = 0; i < dirs.Length; i++) {
                cnt += CountFiles(dirs[i]);
            }
            return cnt + d.GetFiles().Length;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ITagProcessor processor = null;
            ParseResults results;
            TrackNumFrame tnframe;
            TextFrame tframe;

            FileData data = (FileData)e.Node.Tag;

            listView1.SuspendLayout();
            listView1.Items.Clear();

            if (data != null) {
                processor = procman.GetProcessorForFile(data.FileFullName);
                if (processor != null) {
                    results = processor.OpenParse(data.FileFullName);
                    data = new FileData();

                    string[] frameNames = Enum.GetNames(typeof(FrameID));


                    foreach (int i in Enum.GetValues(typeof(FrameID)))
                    {
                        tframe = results.Tag.Frames.GetFirstTextFrame((FrameID)i);
                        if (tframe != null) {
                            listView1.Items.Add(frameNames[i]).SubItems.Add(tframe.Text);
                        } else {
                            listView1.Items.Add(frameNames[i]).SubItems.Add("");
                        }
                    }

                    /*
                    tframe = results.Tag.Frames.GetFirstTextFrame(FrameID.LeadArtist);
                    if (tframe != null) {
                        data.Artist = tframe.Text;
                    } else {
                        tframe = results.Tag.Frames.GetFirstTextFrame(FrameID.OriginalArtist);
                        if (tframe != null) {
                            data.Artist = tframe.Text;
                        }
                    }

                    tframe = results.Tag.Frames.GetFirstTextFrame(FrameID.Album);
                    if (tframe != null) {
                        data.Album = tframe.Text;
                    }

                    tnframe = (TrackNumFrame)results.Tag.Frames.GetFirstFrameByID(FrameID.TrackNum);
                    if (tnframe != null) {
                        data.TrackNum = TrackNumFrame.TrackNumToCanonicalString(tnframe.TrackNumber, tnframe.TrackCount);
                    }

                    tframe = results.Tag.Frames.GetFirstTextFrame(FrameID.Comment);
                    if (tframe != null) {
                        data.Comment = tframe.Text;
                    }
                    */
                } else {
                    //empty list
                }
            } else {
                //empty list
            }

            listView1.ResumeLayout();
        }

    }
}